/*
 
  Copyright (c) 2010, Cristian Costantini - cristian@millemultimedia.it
  
  FileManager is licensed under the terms of the Open Source GPL 3.0 license. 
	
	http://www.gnu.org/licenses/gpl.html
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/


fileManager.ui.UploadFormPanel = function(dataProvider){

    var msg = function(title, msg){
        Ext.Msg.show({
            title: title,
            msg: msg,
            minWidth: 200,
            modal: true,
            icon: Ext.Msg.INFO,
            buttons: Ext.Msg.OK
        });
    };
    
    var fp = new Ext.FormPanel({
		method:'POST',
        fileUpload: true,
        frame: true,
        title: 'File upload',
        height: 140,
        bodyStyle: 'padding: 10px 10px 0 10px;',
        labelWidth: 50,
        
        defaults: {
            anchor: '95%',
            allowBlank: false,
            msgTarget: 'side'
        },
        items: [{
            xtype: 'textfield',
            name: 'destinationPath',
            id: 'destinationPath',
			hidden: true
        }, {
            xtype: 'fileuploadfield',
            id: 'form-file',
            emptyText: 'Select file',
            fieldLabel: 'File',
            name: 'form-file'
        }],
        buttons: [{
            text: 'Reset',
            handler: function(){
                fp.getForm().reset();
            }
        }, {
            text: 'Upload',
            handler: function(){
				if (fp.getForm().isValid()) {
					function getCurrentTime(){
						var currentTime = new Date();
						var hours = currentTime.getHours()
						var minutes = currentTime.getMinutes()
						if (minutes < 10){
							minutes = "0" + minutes;
						}
						var month = currentTime.getMonth() + 1
						var day = currentTime.getDate()
						var year = currentTime.getFullYear()
						
						var time = day+"/"+month+"/"+year+" - "+hours+":"+minutes;
						
						
						return time;
					}
					fp.getForm().submit({
						url: services.uploadproxy,
						
						waitMsg: 'Uploading your file...',
						success: function(ft, o){
							var file = fp.getForm().findField("form-file").getValue();
							msg('Success', 'Processed file "' + file + '" on the server');
							
							var log1 = document.getElementById("logArea").value;
							document.getElementById("logArea").value = getCurrentTime()+' | File '+file+' uploaded to folder '+fp.getForm().findField("destinationPath").getValue()+'\n' + log1;
							
							
						},
						failure: function(ft, o){
							Ext.Msg.show({
								title: 'Error',
								msg: 'An error occurred during file upload',
								buttons: Ext.Msg.OK,
								maxWidth:300,
								minWidth:300
							});
							var file = fp.getForm().findField("form-file").getValue();
							var log1 = document.getElementById("logArea").value;
							document.getElementById("logArea").value = getCurrentTime()+' | Error occurred during file '+file+' upload\n' + log1;
							
						}
						
					});
				}
            }
        }]
    });
    
    return fp;
    
}
